package com.sam.im.heiqun.uis.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.sam.im.heiqun.R;
import com.sam.im.heiqun.uis.beans.ContactBean;
import com.sam.im.heiqun.uis.beans.GlobalRoamingUserBean;
import com.sam.im.heiqun.utils.ToolsUtils;
import com.yuyh.library.utils.GlideUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by wangguodong on 2017/8/17.
 */

public class GlobalRomingUserAdapter extends RecyclerView.Adapter<GlobalRomingUserAdapter.NoticeViewHolder> {

    private Context context ;
    private List<GlobalRoamingUserBean> mList ;
    private LayoutInflater inflater;

    private double mylongitude = 0;
    private double mylatitude = 0;

    public interface OnClickListener{
        void OnClick(GlobalRoamingUserBean noticeBean, int type);//0-item点击；1-邀请点击
    }

    private OnClickListener onClickListener ;
    public void setOnClickListener(OnClickListener mOnClickListener){
        this.onClickListener = mOnClickListener;
    }

    public GlobalRomingUserAdapter(Context context, List<GlobalRoamingUserBean> mList, double mylongitude, double mylatitude){
        this.context = context ;
        this.mylongitude = mylongitude ;
        this.mylatitude = mylatitude ;
        if (null==mList)mList = new ArrayList<>();
        this.mList = mList ;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public NoticeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_global_roming_user, parent, false);
        NoticeViewHolder viewHolder = new NoticeViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(NoticeViewHolder holder, int position) {
        if (position < mList.size()){

            final GlobalRoamingUserBean contactBean = mList.get(position);
            if (null!=contactBean){
                GlideUtils.loadCircleImage(context,contactBean.getHeadUrl(),holder.image_head);

                holder.txt_name.setText(contactBean.getName());
                holder.txt_sign.setText(contactBean.getSign());

                if ("男".equals(contactBean.getSex())){
                    holder.image_sex.setBackgroundResource(R.drawable.male_one);
                }else {
                    holder.image_sex.setBackgroundResource(R.drawable.female);
                }

                double juli = ToolsUtils.getDistance(Double.parseDouble(contactBean.getLongitude()),Double.parseDouble(contactBean.getLatitude())
                        , mylongitude, mylatitude);

                if (juli > 10000){
                    holder.txt_juli.setText((juli/10000)+context.getResources().getString(R.string.mi_wan));
                }else if (juli > 1000){
                    holder.txt_juli.setText((juli/1000)+context.getResources().getString(R.string.mi_qian));
                }else {
                    holder.txt_juli.setText(juli+context.getResources().getString(R.string.mi));
                }
            }
//
            holder.mview.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClickListener.OnClick(contactBean,0);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return null==mList?0:mList.size();
    }

    private String getTime(long time){
        SimpleDateFormat format=new SimpleDateFormat("MM-dd");
        Date d1=new Date(time);
        return format.format(d1);
    }

    class NoticeViewHolder extends RecyclerView.ViewHolder {

        ImageView image_head;
        TextView txt_name;
        TextView txt_sign;
        TextView txt_juli;
        ImageView image_sex;

        private View mview ;

        public NoticeViewHolder(View view) {
            super(view);
            mview = view ;
            image_head = (ImageView) view.findViewById(R.id.image_head);
            txt_name = (TextView) view.findViewById(R.id.txt_name);
            txt_sign = (TextView) view.findViewById(R.id.txt_sign);
            txt_juli = (TextView) view.findViewById(R.id.txt_juli);
            image_sex = (ImageView) view.findViewById(R.id.image_sex);
        }
    }

}
